library(data.table)
library(maps)

## unique site identifier = Monitor (FIPS-SiteID)
## unique monitor identifier = (Monitor, POC)

########################################
###      Medicare + AQS Data        ####
########################################
# These files were created with Link_PM10.R

dat = as.data.frame(fread('Merge_6mile.csv'))  

dat$FIPS = substr(dat$Monitor, 1, 5)


########################################
###  Attainment/Nonattainment Data  ####
########################################
attaindat=read.csv('EPA Nonattainment Table.csv')
attaindat$FIPS=paste(formatC(attaindat$fips_state, width=2, flag="0"), formatC(attaindat$fips_cnty, width=3, flag="0"), sep="")
attaindat=subset(attaindat, pollutant=='PM-10')


##   0 = attainment, 1 = nonattainment if diagnosed between certain years
whichyrs = 1991:1995
attainyrs = as.vector(attaindat[, paste("pw_", whichyrs, sep="")])
attaindat$a = ifelse(apply( attainyrs =="P" | attainyrs =="W", 1, sum)>0, 1, 0) #1=attainment in 1990-1995, 0=else

attaindat=attaindat[, (names(attaindat) %in% c("FIPS", "a"))]


########################################
###        Census Data               ###
########################################
censdat=read.table('Cory-Census-2000-04-11-2011', sep=",", header=TRUE)
names(censdat)[names(censdat)=="Master_county"]="FIPS"
names(censdat)[names(censdat)=="white_rate"]="White_cens"
names(censdat)[names(censdat)=="black_rate"]="Black_cens"
names(censdat)[names(censdat)=="Other_rate"]="Other_cens"
names(censdat)[names(censdat)=="Hispanic_rate"]="Hispanic_cens"
names(censdat)[names(censdat)=="Female_rate"]="Female_cens"
censdat$FIPS = as.character(formatC(censdat$FIPS, format="d", width=5, flag="0"))



########################################
###         Weather Data             ###
########################################
weather=read.csv('Annual-weather-11-05-2012.csv', sep=",", header=TRUE)
weather=subset(weather, year==1990)
weather$FIPS = as.character(formatC(weather$FIPS, format="d", width=5, flag="0"))




########################################
###            Merges                ###
########################################


d1 = merge(dat, attaindat, by="FIPS", all.x=TRUE)
d1$a[is.na(d1$a)] = 0

d2 = merge(d1, censdat, by="FIPS", all.x=TRUE)

d3 = merge(d2, weather, by = "FIPS", all.x=TRUE)



########################################
###            Output                ###
########################################
alldat = d3
save(alldat, file = 'alldata.RData')






